﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NewsInformation.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The news information.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;

    /// <summary>
    /// The news information.
    /// </summary>
    [DataContract]
    public class NewsInformation
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the archive location.
        /// </summary>
        [DataMember]
        public string ArchiveLocation { get; set; }

        /// <summary>
        /// Gets or sets the comments.
        /// </summary>
        [DataMember]
        public string Comments { get; set; }

        /// <summary>
        /// Gets or sets the creator.
        /// </summary>
        [DataMember]
        public Guid Creator { get; set; }

        /// <summary>
        /// Gets or sets the files.
        /// </summary>
        [DataMember]
        public List<Guid> Files { get; set; }

        /// <summary>
        /// Gets or sets the funds.
        /// </summary>
        [DataMember]
        public List<Guid> Funds { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether is information about funds.
        /// </summary>
        [DataMember]
        public bool IsInformationAboutFunds { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether is information about progress.
        /// </summary>
        [DataMember]
        public bool IsInformationAboutProgress { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether is information about results.
        /// </summary>
        [DataMember]
        public bool IsInformationAboutResults { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the owner projects.
        /// </summary>
        [DataMember]
        public List<Guid> OwnerProjects { get; set; }

        /// <summary>
        /// Gets or sets the time of activity.
        /// </summary>
        [DataMember]
        public DateTime? TimeOfActivity { get; set; }

        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        [DataMember]
        public Guid Type { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="newsInformation">
        /// The news information.
        /// </param>
        /// <returns>
        /// The <see cref="NewsInformation"/>.
        /// </returns>
        public static NewsInformation Convert(Models.Domain.NewsInformation newsInformation)
        {
            if (newsInformation != null)
            {
                var dto = new NewsInformation
                              {
                                  ID = newsInformation.ID, 
                                  ArchiveLocation = newsInformation.ArchiveLocation, 
                                  Comments = newsInformation.Comments, 
                                  Creator = newsInformation.Creator.ID, 
                                  Name = newsInformation.Name, 
                                  TimeOfActivity = newsInformation.TimeOfActivity, 
                                  Type = newsInformation.Type.ID, 
                                  IsInformationAboutFunds = newsInformation.IsInformationAboutFunds, 
                                  IsInformationAboutProgress = newsInformation.IsInformationAboutProgress, 
                                  IsInformationAboutResults = newsInformation.IsInformationAboutResults
                              };

                if (newsInformation.OwnerProjects != null && newsInformation.OwnerProjects.Any())
                {
                    dto.OwnerProjects = new List<Guid>();
                    foreach (Models.Domain.Project proj in newsInformation.OwnerProjects)
                    {
                        dto.OwnerProjects.Add(proj.ID);
                    }
                }

                if (newsInformation.Files != null && newsInformation.Files.Any())
                {
                    dto.Files = newsInformation.Files.Select(x => x.ID).ToList();
                }

                if (newsInformation.Funds != null && newsInformation.Funds.Any())
                {
                    dto.Funds = newsInformation.Funds.Select(x => x.ID).ToList();
                }

                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;
    using ServiceLibrary.Services.Results;
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The news information query.
    /// </summary>
    [DataContract]
    public class NewsInformationQuery : IQuery
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the default fields.
        /// </summary>
        [DataMember]
        public DefaultSearchFields DefaultFields { get; set; }
        
        /// <summary>
        /// Gets or sets the default fields.
        /// </summary>
        [DataMember]
        public List<Guid> WithFunds { get; set; }

        /// <summary>
        /// Gets or sets the default fields.
        /// </summary>
        [DataMember]
        public Guid? WithType { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new NewsInformationActioner(hsession, null);

            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The news information result.
    /// </summary>
    [DataContract]
    public class NewsInformationResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="NewsInformationResult"/> class.
        /// </summary>
        public NewsInformationResult()
        {
            this.List = new List<NewsInformation>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<NewsInformation> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create news information command.
    /// </summary>
    [DataContract]
    public class CreateNewsInformationCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public NewsInformation Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new NewsInformationActioner(hsession, activeUser);
            actioner.CreateNewsInformation(this);
        }

        #endregion
    }

    /// <summary>
    /// The update news information command.
    /// </summary>
    [DataContract]
    public class UpdateNewsInformationCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public NewsInformation Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new NewsInformationActioner(hsession, activeUser);
            actioner.UpdateNewsInformation(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete news information command.
    /// </summary>
    [DataContract]
    public class DeleteNewsInformationCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Guid? Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new NewsInformationActioner(hsession, activeUser);
            actioner.DeleteNewsInformation(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete news information command.
    /// </summary>
    [DataContract]
    public class DeleteNewsInformationFileCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the owner newsinformation.
        /// </summary>
        [DataMember]
        public Guid? OwnerNewsID { get; set; }

        /// <summary>
        /// Gets or sets the file id.
        /// </summary>
        [DataMember]
        public Guid? FileID { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new NewsInformationActioner(hsession, activeUser);
            actioner.DeleteNewsInformationFile(this);
        }

        #endregion
    }
}